﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using exsportance.Silverlight.Common.Utils;
using System.Text.RegularExpressions;

namespace exsportance.Silverlight.Validate.Validators
{
    /// <summary>
    /// Validator for fields where a value must be a date
    /// </summary>
    public class DateValidator : BaseValidator
    {
        #region members
        /// <summary>
        /// Validation failure message key for when the value does not appear to be a valid date
        /// </summary>
        private const string INVALID = "dateInvalid";

        /// <summary>
        /// Validation failure message key for when the value does not fit the given dateformat or locale
        /// </summary>
        private const string FALSEFORMAT = "dateFalseFormat";

        protected Dictionary<string, string> _messagesTemplates = new Dictionary<string, string>(){
         {DateValidator.INVALID,"'%value%' does not appear to be a valid date"},
         {DateValidator.FALSEFORMAT,"'%value%' does not fit given date format"},
        };
        #endregion

        #region properties
        /// <summary>
        /// Optional date format
        /// </summary>
        public string Format { get; set; }
        #endregion

        #region constructor
        public DateValidator()
            : base()
        {
        }

        public DateValidator(string format)
            : this()
        {
            Format = format;
        }
        #endregion

        #region protected methods
        protected override bool CheckValid(object value)
        {
            string valueString = value as string;

            rememberValue(valueString);

            if (!Format.IsNullOrEmpty())
            {
                DateTime dateTime;
                if (!DateTime.TryParse(valueString, out dateTime))
                {
                    raiseMessage(DateValidator.INVALID);
                    return false;
                }
            }
            else
            {

                DateTime dateTime;
                if (!DateTime.TryParse(valueString, out dateTime))
                {
                    raiseMessage(DateValidator.INVALID);
                    return false;
                }
            }

            return true;
        }


        protected override System.Collections.Generic.Dictionary<string, string> getMessageTemplates()
        {
            return _messagesTemplates;
        }
        #endregion

    }
}
